package com.cy.exception;

import com.cy.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
        if (e instanceof ServiceException) {
            ServiceException ex = (ServiceException) e;
            if (Objects.nonNull(ex.getCode())) {
                return JsonResult.err(ex.getCode(), ex.getMessage());
            }
            return JsonResult.err(ex.getMessage());
        }
        e.printStackTrace();
        return JsonResult.err("服务器忙，请稍后再尝试！");
    }

}
